package scut.carson_ho.database_demo;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MyData extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.data);

        findId();
        init();

    }

    private EditText inname, inkind, inbirth, checkName;
    TextView toInsert, check;

    private void findId() {
        inname = findViewById(R.id.name);
        inkind = findViewById(R.id.kind);
        inbirth = findViewById(R.id.birth);
        toInsert = findViewById(R.id.insert);
        checkName = findViewById(R.id.check_name);//输入要查询的名字
        check = findViewById(R.id.check);//查询按钮
    }

    private void init() {

        toInsert.setOnClickListener(one);
        check.setOnClickListener(one);

    }

    //查询数据库
/*
    private void selectSQL() {
        //表的每一列名称
        String dataname = inname.getText().toString();
        String kind = inkind.getText().toString();
        String birth = inbirth.getText().toString();
        SQLiteDatabase db = openOrCreateDatabase("newItem.db", MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS user (name text PRIMARY KEY AUTOINCREMENT,kind varchar(40),birth varhcar(40))");
        Cursor cursor = db.query("user", new String[]{"name, kind, birth"}, "name=?", new String[]{dataname}, null, null, null, null);

        cursor.moveToFirst();
        inname.setText(cursor.getString(cursor.getColumnIndex("name")));
        inbirth.setText(cursor.getString(cursor.getColumnIndex("birth")));
        inkind.setText(cursor.getString(cursor.getColumnIndex("kind")));
//        tvPwd.setText(cursor.getString(cursor.getColumnIndex("Password")));
        cursor.close();

    }
*/

    /**
     * 创建数据库
     *
     */
    private void sql(){
        String pname = inname.getText().toString();
        String pkind = inkind.getText().toString();
        String pbirth = inbirth.getText().toString();
        SQLiteDatabase db = openOrCreateDatabase("user.db", MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS user (name varchar(40) PRIMARY KEY,kind varchar(40),birth varhcar(40))");
        String sql = "insert into user(name," + "kind," + "birth) values('" + pname + "'," + "'" + pkind + "'," + "'" + pbirth + "')";
//
        try{
            db.execSQL(sql);
            Toast.makeText(MyData.this, "保存成功", Toast.LENGTH_SHORT).show();
        }catch (Exception e){
            Toast.makeText(MyData.this,"信息错误",Toast.LENGTH_SHORT).show();
        }
    }
    private Intent intent;

    View.OnClickListener one = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.insert:
                        sql();
                    break;
                case R.id.check:
                    intent = new Intent(MyData.this, MyLook.class);
                    String selectname = checkName.getText().toString();
                    intent.putExtra("1",selectname);
                    startActivity(intent);

                    Toast.makeText(MyData.this, "找到该信息", Toast.LENGTH_SHORT).show();
                    break;
            }

        }
    };

}
